package org.mics.sms.service.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.mics.sms.config.AliYunSmsProperties;
import org.mics.sms.entity.SmsRecordDO;
import org.mics.sms.exception.SmsException;
import org.mics.sms.helper.AliYunSmsUtils;
import org.mics.sms.repository.SmsRecordRepository;
import org.mics.sms.service.SmsRecordService;
import org.springframework.stereotype.Service;

/**
 * 短信内容
 * @author mics
 * @date 2022-5-7
 * @version  1.0
 */
@Service
public class SmsRecordServiceImpl implements SmsRecordService{
	@Resource
	private SmsRecordRepository smsRecordRepository;
	@Resource
	private AliYunSmsProperties aliYunSmsProperties;
	
	/**
	 * 短信成功状态
	 */
    private static final String SUCCESS = "OK";
	
	@Override
	public String send(String mobile,String templeteCode,String templeteParam) {
		Map<String,Object> result = AliYunSmsUtils.sendSms(aliYunSmsProperties, mobile,templeteCode,templeteParam);
		if(SUCCESS.equals(result.get("Code"))) {
			//添加验证码短信记录
			SmsRecordDO smsRecordDO =  new SmsRecordDO();
			smsRecordDO.setMobile(mobile);
			smsRecordDO.setTempleteCode(templeteCode);
			smsRecordDO.setMessage(templeteParam);
			smsRecordDO = smsRecordRepository.save(smsRecordDO);
			return smsRecordDO.getId();
		}else {
			throw new SmsException(String.valueOf(result.get("Message")));
		}
	}

	
}
